package com.mtjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.mtjx.entitys.LoginUser;
//import com.mtjx.entitys.User;

import com.mtjx.entitys.entitys.LoginUser;
import com.mtjx.entitys.entitys.User;
import com.mtjx.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 33117
 * @version 1.0
 * @data 2025/8/29 11:02
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1. 查询用户信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        User user = userService.getOne(lambdaQueryWrapper);
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户不存在或不存在");
        }
        // 查询对应的权限信息
        List<String> list = userService.queryAuthorization(username);
        // 把数据封装成UserDetails对象并返回
        return new LoginUser(user, list);
    }
}
